package com.shuozi.keyboardpopup;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.databinding.BindingAdapter;

import java.util.ArrayList;
import java.util.List;

public class KeyboardPopupHelper {

    public static   List<EditText> editTextList=new ArrayList<>();

    public static  List<EditText> getEditTextList() {

        if (editTextList == null) {
            editTextList = new ArrayList<>();

        }
        return editTextList;
    }

    public static boolean dismiss(BaseKeyboardPopup keyboardPopupWindow){
        if (keyboardPopupWindow != null && keyboardPopupWindow.isShowing()) {
            keyboardPopupWindow.dismiss();
            return true;
        }
        return false;
    }

    public static void releaseResources(BaseKeyboardPopup keyboardPopupWindow) {
        KeyboardPopupHelper.getEditTextList().clear();
        if (keyboardPopupWindow != null) {
            keyboardPopupWindow.releaseResources();
        }
    }

    public static void  addOnfoursListener(View view, View.OnFocusChangeListener listener) {
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                if (viewchild instanceof EditText){
                    Log.e("aa","addOnfoursListener");
                    viewchild.setOnFocusChangeListener(listener);
                }
                addOnfoursListener(viewchild,listener);
                //再次 调用本身（递归）
            }
        }
    }
    private List<View> getAllChildViews(View view) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                if (viewchild instanceof EditText){

                }
                allchildren.add(viewchild);
                //再次 调用本身（递归）
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }

    @BindingAdapter(value = {"bindingNumKeyboard"}, requireAll = false)
    public static void bindingNumKeyboard(EditText editText, final Boolean bindingNumKeyboard) {


            if (bindingNumKeyboard){
                KeyboardPopupHelper.getEditTextList().add(editText);
            }



    }
}
